<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主域名" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          placeholder="请输入主域名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="domainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主域名" align="center" prop="domain" />
      <el-table-column label="开启状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主域名" prop="domain">
          <el-input v-model="domain.domain" placeholder="请输入域名" />
        </el-form-item>
        <el-form-item label="启用">
          <el-switch v-model="domain.status" active-value="0" inactive-value="1"></el-switch>
        </el-form-item>
        <el-form-item label="">
          <el-card class="box-card" style="flex-direction: column;">
            <div slot="header" class="clearfix">
              <span>跳转地址</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="addDomainLink">添加</el-button>
            </div>
            <div v-for="(link, linkIndex) in domain.links" style="display: flex; flex-direction: column;background: #e9ecf3;padding: 10px 10px;margin: 20px 0;">
              <div style="display: flex;">
                <el-form-item label="地址" prop="link.domain" style="margin-bottom: 20px; flex-grow: 1;">
                  <el-input v-model="link.domain" placeholder="请输入域名名称" />
                </el-form-item>
                <el-form-item label="启用">
                  <el-switch v-model="link.status" active-value="0" inactive-value="1"></el-switch>
                </el-form-item>
              </div>
              <div class="text item" style="margin-bottom:10px; display: flex;flex-grow: 1; flex-direction: column;">
                <div>
                  <el-card class="box-card">
                    <div slot="header" class="clearfix">
                      <span>落地页</span>
                      <el-button style="float: right; padding: 3px 0" type="text" @click="addDomainPage(linkIndex)">添加</el-button>
                    </div>
                    <div>
                      <div v-for="(page, pageIndex) in domain.links[linkIndex].pages" :key="o" class="text item" style="display: flex;">
                        <el-form-item label="落地页" prop="page.domain" style="margin-bottom: 20px; flex-grow: 1;">
                          <el-input v-model="page.domain" placeholder="请输入域名名称" />
                        </el-form-item>
                        <el-form-item label="启用">
                          <el-switch v-model="page.status" active-value="0" inactive-value="1"></el-switch>
                        </el-form-item>
                      </div>
                    </div>
                  </el-card>
                </div>
              </div>

            </div>
          </el-card>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { domainPage, addDomain, updateDomain, getDomain } from "@/api/system/domain";

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 角色表格数据
      domainList: [],
      //主域名
      domain: {
        domain: null,
        status: "0",
        links: [
          {
            domain: '',
            status: "0",
            pages:[{
              domain: '',
              status: "0",
            }]
          }
        ]
      },
      // 弹出层标题
      title: "",
      // 开启显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "跳转地址不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addDomainLink(){
      const d = {domain:'', status: "0", pages:[{domain:'', status: "0"}]}
      this.domain.links.push(d);
      console.log(this.domain);
    },
    addDomainPage(linkIndex){
      const d = {domain:'', status: "0"}
      this.domain.links[linkIndex].pages.push(d)
    },
    getList() {
      this.loading = true;
      domainPage(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.domainList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.domain = {
        domain: null,
          status: "0",
          links: [
          {
            domain: '',
            status: "0",
            pages:[{
              domain: '',
              status: "0",
            }]
          }
        ]
      }
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加域名配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const domainId = row.id
      getDomain(domainId).then(response => {
        if(response.code === 200) {
          const resDomain = response.domain;
          console.log('===resDomain===', resDomain);
          this.domain = resDomain;
          console.log(this.domain)
          this.open = true;
          this.title = "修改域名配置";
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.domain.id !== undefined) {
            updateDomain(this.domain).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDomain(this.domain).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal.confirm('开启确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
        return delPost(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
